package com.fasttreatment.zzr.demo.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.androidlibrary.base.BaseActivity;
import com.android.androidlibrary.router.Router;
import com.fasttreatment.zzr.demo.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.qrcode.core.BGAQRCodeUtil;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;

/**
 * Created by zzr on 17/1/16.
 */

public class CreateQrcodeActivity extends BaseActivity {
    @BindView(R.id.iv_chinese)
    ImageView ivChinese;
    @BindView(R.id.iv_chinese_logo)
    ImageView ivChineseLogo;
    @BindView(R.id.toolbar_left_btn)
    Button mToolbarLeftBtn;

    @Override
    public void initData(Bundle savedInstanceState) {
        createChineseQRCode();
        createChineseQRCodeWithLogo();
    }

    @Override
    public void setListener() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_createqrcode;
    }

    private void createChineseQRCode() {
        new AsyncTask<Void, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(Void... params) {
                return QRCodeEncoder.syncEncodeQRCode("zzr", BGAQRCodeUtil.dp2px(CreateQrcodeActivity.this, 150));
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                if (bitmap != null) {
                    ivChinese.setImageBitmap(bitmap);
                } else {
                    Toast.makeText(CreateQrcodeActivity.this, "生成中文二维码失败", Toast.LENGTH_SHORT).show();
                }
            }
        }.execute();
    }

    private void createChineseQRCodeWithLogo() {
        new AsyncTask<Void, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(Void... params) {
                Bitmap logoBitmap = BitmapFactory.decodeResource(CreateQrcodeActivity.this.getResources(), R.mipmap.ic_launcher);
                return QRCodeEncoder.syncEncodeQRCode("zzr", BGAQRCodeUtil.dp2px(CreateQrcodeActivity.this, 150), Color.parseColor("#ff0000"), logoBitmap);
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                if (bitmap != null) {
                    ivChineseLogo.setImageBitmap(bitmap);
                } else {
                    Toast.makeText(CreateQrcodeActivity.this, "生成带logo的中文二维码失败", Toast.LENGTH_SHORT).show();
                }
            }
        }.execute();
    }

}
